<template>
	<view>
		<view class="search-top">
			<uni-search-bar @input="change" @change='rest' placeholder="自定义背景色" bgColor="#fff" :radius="100" />
		</view>
		<!-- 搜索相关 -->
		<view class='sug-list'>
			<view class='sug-items' v-if='searchResult.length!==0' v-for='(item,i) in searchResult' :key='i'
				@tap="getId(item)">
				<view class='text'>
					{{item.goods_name}}
				</view>
				<uni-icons type="arrowright" size='16'></uni-icons>
			</view>
			<!-- 搜索历史 -->
			<view class="history" v-if='searchResult.length==0'>
				<view><text>搜索历史</text><uni-icons type="trash" size="17" @tap='clear'></uni-icons></view>
				<view class='more'>
					<uni-tag :text="item" v-for="(item,i) in newHl" :key='i' @tap='seeGoods(item)'></uni-tag>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				searchResult: [],
				historyList: [],
				db: 1
			};
		},
		methods: {
			change(e) {

				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchlist()
				}, 500)
			},
			async getSearchlist() {

				if (this.kw == '') {
					this.searchResult = []
					return
				}
				const { data: res } = await uni.$http.get('public/v1/goods/qsearch', { query: this.kw })
				while (res.meta.status !== 200) return uni.$showMsg()
				this.searchResult = res.message
				this.saveHL()
			},
			getId(item) {
				uni.navigateTo({
					url: `/subpkg/goods_detail/goods_detail?id=${item.goods_id}`
				})
				console.log('ch');

			},
			rest() {
				if (this.kw == null) {
					this.searchResult = []
				}
				console.log('ch');
			},
			saveHL() {
				if (this.historyList.includes(this.kw) !== true) {
					this.historyList.push(this.kw)
				}
			},
			// clear() {
			// 	// this.historyList = [] item
			// },
			seeGoods(it) {
				uni.navigateTo({
					url: `/subpkg/goods_detail/goods_detail?kw=${it}`
				})
			}


		},
		computed: {
			newHl() {
				return [...this.historyList].reverse()
			}
		}
	}

</script>

<style lang="scss">
	.sug-list {
		padding: 0 5px;

		.sug-items {
			font-size: 12px;
			padding: 13px 0;
			border-radius: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1px solid #efefef;

			.text {
				white-space: nowrap;
				text-overflow: ellipsis;
				overflow: hidden;
				margin-right: 3px;
			}
		}
	}

	.more {
		display: flex;
		flex-wrap: wrap;

		.uni-tag {
			display: block;
			margin: 5px 10px !important;
		}
	}

	.uniui-transh {}

</style>

